package com.example.pertecttime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.pertecttime.R;
import com.example.pertecttime.entity.FriendBean;
import com.example.pertecttime.entity.GroupBean;

import java.util.List;

/**
 * Created by 半生瓜 on 2017/7/25.
 */

public class MyListViewAdapter extends BaseExpandableListAdapter {
    private List<GroupBean> list;
    private Context context;
    public MyListViewAdapter(List<GroupBean> list, Context context){
        this.list=list;
        this.context=context;
    }
    class GroupHolder {
        ImageView img;
        TextView groud;
    }

    class ChildHolder {
        ImageView head;
        TextView name, sign;
    }
    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder;
        if (convertView == null) {
            holder = new GroupHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.groulistview, null);
            holder.groud = (TextView) convertView
                    .findViewById(R.id.groud);
            holder.img = (ImageView) convertView.findViewById(R.id.imgoc);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        holder.groud.setText(list.get(groupPosition).getGroupName());
        if (isExpanded) {
            holder.img.setImageResource(R.mipmap.qb_down);
        } else {
            holder.img.setImageResource(R.mipmap.qb_right);
        }
        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildHolder holder;
            if (convertView == null) {
                holder = new ChildHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.friendlistview, null);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.sign = (TextView) convertView.findViewById(R.id.sign);
                convertView.setTag(holder);
            } else {
                holder = (ChildHolder) convertView.getTag();
            }
            FriendBean cb = list.get(groupPosition).getList().get(childPosition);
            holder.name.setText(cb.getName());
            holder.sign.setText("[签名]"+cb.getSign());
            return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
